import 'package:dmz/base/pageWidget/base_stateful_widget.dart';
import 'package:dmz/base/widget/defult_widget.dart';
import 'package:dmz/string_res/str_res_keys.dart';

import 'package:dmz/user/user_defurl.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

import '../../../base/widget/defult_text_field.dart';

class SetingWidgets {
  static Widget headImage() {
    //头像
    return DefaultNetWorkImage(EMIBUserManger().user?.picture ?? '',
            width: 116.sp, height: 116.sp)
        .didClipRRectAll(116.sp)
        .marginOnly(right: 20.sp);
  }

  static Widget topWidgets(Function(int tag) click) {
    List<String> titles = [
      '头像'.tr,
      '昵称'.tr,
      '更换手机号'.tr,
      (EMIBUserManger().user?.password_set ?? false)
          ? '修改登录密码'.tr
          : '设置登录密码'.tr,
      '指纹登录'.tr
    ];
    List<String> contents = [
      '',
      EMIBUserManger().user?.name ?? '',
      EMIBUserManger().user?.phone ?? '',
      '',
      ''
    ];
    return Container(
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      DefaultText('个人资料'.tr, 32.sp, textColor: const Color(0xB3FFFFFF))
          .marginOnly(left: 32.sp, bottom: 22.sp, top: 22.sp),
      Column(
          mainAxisSize: MainAxisSize.min,
          children: List.generate(
              titles.length,
              (index) => pageItem(index, titles[index],
                  type: index == 0
                      ? 1
                      : index == 4
                          ? 2
                          : 0,
                  hitLine: index == 0,
                  content: contents[index],
                  hitIcon: index == 4,
                  child: index == 0 ? headImage() : null,
                  click: click)))
    ]));
  }

  static mindleWidget(Function(int tag) click, List<bool> types) {
    List<String> titles = [
      '异常震动'.tr,
      '切断电源'.tr,
      '充电检测'.tr,
      '防盗报警'.tr,
      '故障消息'.tr
    ];
    List<String> hitStrs = [
      '车辆发生异常震动'.tr,
      '未充满电切断电源'.tr,
      '充电进程中电池温度，电压超过阀值'.tr,
      '车辆异常移动或设备被破坏'.tr,
      '需具体车辆开启上报功能'.tr
    ];
    Widget content() {
      return Column(
          mainAxisSize: MainAxisSize.min,
          children: List.generate(
              titles.length,
              (index) => pageItem(index + 5, titles[index],
                  hitLine: index == 0,
                  hitStr: hitStrs[index],
                  type: 2,
                  hitIcon: true,
                  select: types[index],
                  click: click)));
    }

    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      DefaultText('消息设置'.tr, 32.sp, textColor: const Color(0xB3FFFFFF))
          .marginOnly(left: 32.sp, bottom: 22.sp, top: 22.sp),
      Container(
        padding: EdgeInsets.only(
            left: 20.sp, right: 20.sp, top: 34.sp, bottom: 34.sp),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(56.sp), // 设置圆角半径
            border: Border.all(
              color: Color(0xFF979797), // 设置边框颜色
              width: 2.sp, // 设置边框宽度
            )),
        child: Column(children: [
          DefaultText(SR.openHit.tr, 32.sp, textColor: Color(0xB3FFFFFF))
        ]),
      ).marginOnly(left: 32.sp, right: 32.sp, bottom: 32.sp),
      content()
    ]);
  }

  static Widget bottomWidget(Function(int tag) click) {
    List<String> titles = ['多语言切换'.tr, '意见反馈'.tr, '关于我们'.tr, '注销账号'.tr];
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      DefaultText('应用信息'.tr, 32.sp, textColor: const Color(0xB3FFFFFF))
          .marginOnly(left: 32.sp, bottom: 54.sp, top: 54.sp),
      Column(
          mainAxisSize: MainAxisSize.min,
          children: List.generate(
              titles.length,
              (index) => pageItem(10 + index, titles[index],
                  hitLine: index == 0, click: click))),
      DefaultBtn(click: () => click(14), title: '退出登录'.tr)
          .marginOnly(left: 40, right: 40, top: 80.sp, bottom: 112.sp)
    ]);
  }

  static inputWidget(
      TextEditingController controller, Function(String) valueChange,
      {int inputLenth = 500}) {
    return Stack(alignment: Alignment.bottomRight, children: [
      Container(
              color: const Color(0xFF434650),
              height: 256.sp,
              child: DefultTextField(32.sp,
                      hintText: '请输入'.tr,
                      maxLine: 99,
                      controlle: controller,
                      onChanged: valueChange)
                  .marginOnly(left: 32.sp))
          .didClipRRectAll(52.sp),
      DefaultText('${controller.text.length}/$inputLenth', 32.sp,
              textColor: const Color(0xFFEBEBEB))
          .marginOnly(right: 34.sp, bottom: 24.sp)
    ]);
  }

  static Widget imageSelectWidget(String title, List<AssetEntity> imagesDatas,
      Function() addImage, Function(int) clean) {
    addItem() {
      return Container(
              alignment: Alignment.center,
              color: Colors.transparent,
              child: Image.asset('assets/images/home/set/add_image.png',
                  width: 160.sp, height: 160.sp))
          .addGestureDetector(addImage);
    }

    imageItem(AssetEntity data, Function clean) {
      return Stack(alignment: Alignment.topRight, children: [
        Container(
            width: 200.sp,
            height: 200.sp,
            color: Colors.transparent,
            child: AssetEntityImage(data, fit: BoxFit.fill)),
        Image.asset('assets/images/login/input_clean.png',
                width: 48.sp, height: 48.sp)
            .addGestureDetector(() => clean())
      ]);
    }

    var have = imagesDatas.length == 6;
    return Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          DefaultText('$title（${imagesDatas.length}/6）', 32.sp,
                  textColor: const Color(0xB3FFFFFF))
              .marginOnly(bottom: 70.sp),
          GridView.builder(
              shrinkWrap: true,
              primary: false,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3, // 设置每行显示的item数量
                crossAxisSpacing: 82.sp, // 每列之间的间距
                mainAxisSpacing: 48.sp, // 每行之间的间距
                childAspectRatio: 1.0, // 子项的宽高比
              ),
              itemCount: have ? 6 : imagesDatas.length + 1,
              itemBuilder: (context, index) {
                return index == imagesDatas.length
                    ? addItem()
                    : imageItem(imagesDatas[index], () => clean(index));
              }),
        ]).marginOnly(left: 32.sp, right: 32.sp);
  }

  static Widget pageItem(int tag, String title,
      {int type = 0,
      String? content,
      String? hitStr,
      Widget? child,
      Function(int tag)? click,
      bool select = false,
      bool hitLine = false,
      bool hitIcon = false}) {
    return Container(
        color: const Color(0xFF434650),
        child: Column(children: [
          Container(
                  color: hitLine ? Colors.transparent : Color(0xFFD8D8D8),
                  height: 2.sp)
              .marginOnly(left: 32.sp, right: 32.sp),
          Row(children: [
            Expanded(
                child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                  DefaultText(title, 40.sp,
                      textColor: const Color(0xFFF9F9F9),
                      fontWeight: FontWeight.w700),
                  if (hitStr != null)
                    DefaultText(hitStr, 32.sp,
                            textColor: const Color(0xB3FFFFFF))
                        .marginOnly(top: 24.sp),
                ])),
            Container(
                height: type == 0 ? 154.sp : null,
                color: Colors.transparent,
                child: Row(mainAxisAlignment: MainAxisAlignment.end, children: [
                  if (type == 1) child!,
                  if (type == 0)
                    DefaultText(content ?? '', 32.sp,
                            textColor: const Color(0xFFF9F9F9))
                        .marginOnly(right: 20.sp),
                  if (type == 2)
                    Image.asset(
                            'assets/images/home/set/btn_${select ? 'open' : 'close'}.png',
                            width: 110.sp,
                            height: 66.sp)
                        .addGestureDetector(() => click!(tag)),
                  if (!hitIcon)
                    Image.asset('assets/images/home/set/right_icon.png',
                        width: 18.sp, height: 36.sp)
                ])).addGestureDetector(() => type == 2 ? null : click!(tag)),
          ]).paddingOnly(left: 32.sp, right: 32.sp).marginOnly(
              top: type == 1
                  ? 20.sp
                  : type == 0
                      ? 0.sp
                      : 48.sp,
              bottom: type == 1
                  ? 20.sp
                  : type == 0
                      ? 0.sp
                      : 48.sp),
        ]));
  }
}
